package com.ywu.bpmn.standard.listener;

import com.ywu.bpmn.spi.dto.YwuBpmnMqDto;
import com.ywu.bpmn.standard.produce.YwuBpmnProduce;
import com.ywu.common.gson.GsonUtils;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName YwuProcessStartListener
 * @Description TODO
 * @Author GroundDemo
 * @Date 2025/4/23 19:48
 * @Version 1.0
 **/
@Component
public class YwuProcessStartListener implements ExecutionListener {
    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private YwuBpmnProduce produce;

    @Override
    public void notify(DelegateExecution delegateExecution) {
        Object instanceId = delegateExecution.getVariable("instanceId");
        YwuBpmnMqDto ywuBpmnMqDto = new YwuBpmnMqDto();
        ywuBpmnMqDto.setInstanceId((String) instanceId);
        ywuBpmnMqDto.setBpmnInstanceId(delegateExecution.getProcessInstanceId());
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(delegateExecution.getProcessDefinitionId()).singleResult();
        ywuBpmnMqDto.setKey(processDefinition.getKey());
        // 发送MQ
        produce.setKey("process-start");
        produce.produce(GsonUtils.serialize(ywuBpmnMqDto));
    }
}
